/*
 * Copyright (C) 2017, 2019 Samuel Wall
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package uk.co.samuelwall.materialtaptargetprompt.sample;

import ohos.aafwk.ability.Ability;
import ohos.agp.animation.Animator;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.element.VectorElement;
import ohos.agp.window.dialog.CommonDialog;
import ohos.app.Context;
import ohos.global.resource.NotExistException;
import ohos.global.resource.WrongTypeException;
import uk.co.samuelwall.materialtaptargetprompt.DialogResourceFinder;
import uk.co.samuelwall.materialtaptargetprompt.MaterialTapTargetPrompt;
import uk.co.samuelwall.materialtaptargetprompt.sample.utils.NumUtil;

import java.io.IOException;

/**
 * Example that shows a prompt when displayed.
 *
 * @since 2021-06-21
 */
public class BottomSheetDialogFragmentExample extends CommonDialog {
    private Context mContext;
    private Ability mAbility;
    private boolean isShow = true;

    /**
     * BottomSheetDialogFragmentExample
     *
     * @param ability ability
     */
    public BottomSheetDialogFragmentExample(Ability ability) {
        super(ability.getContext());
        mContext = ability.getContext();
        mAbility = ability;
    }

    @Override
    protected void onCreate() {
        super.onCreate();
        setAutoClosable(false);
        setTransparent(true);
        Component contentView = LayoutScatter.getInstance(mContext).parse(ResourceTable.Layout_fragment_bottom_sheet,
                null, false);
        setContentCustomComponent(contentView);
        DialogResourceFinder dialogResourceFinder = new DialogResourceFinder(mAbility,
                (DependentLayout) getContentCustomComponent().findComponentById(ResourceTable.Id_dialog_dl));
        try {
            new MaterialTapTargetPrompt.Builder(dialogResourceFinder, null)
                    .setPrimaryText(ResourceTable.String_search_prompt_title)
                    .setSecondaryText(ResourceTable.String_search_prompt_description)
                    .setAnimationCurveType(Animator.CurveType.ACCELERATE_DECELERATE)
                    .setMaxTextWidth(ResourceTable.Float_tap_target_menu_max_width)
                    .setIcon(new VectorElement(mContext, ResourceTable.Graphic_ic_search))
                    .setTarget(ResourceTable.Id_bs_search)
                    .setPromptStateChangeListener((prompt, state) -> {
                        if (state == MaterialTapTargetPrompt.STATE_NON_FOCAL_PRESSED) {
                            hide();
                        } else if (state == MaterialTapTargetPrompt.STATE_FOCAL_PRESSED
                                || state == MaterialTapTargetPrompt.STATE_FOCAL_BACKGROUND_PRESSED) {
                            mAbility.getMainTaskDispatcher().delayDispatch(() -> isShow = false, NumUtil.INT_100);
                        }
                    })
                    .show();
        } catch (NotExistException | WrongTypeException | IOException e) {
            String error = e.toString();
        }
        contentView.setClickedListener(component -> {
            if (!isShow) {
                hide();
            }
        });
    }
}
